package com.soft.admin.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.soft.admin.common.ElementConst;
import com.soft.admin.dto.ComboDTO;
import com.soft.admin.dto.TeachDTO;
import com.soft.admin.service.TeachService;
import com.soft.dao.TeachDao;
import com.soft.model.Channel;
import com.soft.model.Teach;
import com.soft.model.User;
import com.soft.support.PageParam;
import com.soft.support.Pager;
import com.soft.util.DateUtil;
import com.soft.util.JsonUtil;
import com.soft.util.RequestUtil;

@SuppressWarnings("unchecked")
@Component
public class TeachServiceImpl implements TeachService {
	
	private TeachDao teachDao;
	
	@Autowired
	public void setTeachDao(TeachDao teachDao) {
		this.teachDao = teachDao;
	}

	
	@Transactional(readOnly = true)
	public String get(TeachDTO params, PageParam page) {
		List<Criterion> values = new ArrayList<Criterion>();
		values.add(Restrictions.like("title", params.getTitle(), MatchMode.ANYWHERE));
		if (params.getChannel() != -1) {
			values.add(Restrictions.eq("channel.id", params.getChannel()));
		}
		if (params.getState() != -1) {
			values.add(Restrictions.eq("state", params.getState()));
		}
		Pager teachLst = this.teachDao.findForPager(Teach.class, new Order[] { Order.desc("level"), Order.desc("id") }, 
				page.getStart(), page.getLimit(), values);
		Pager pageModel = new Pager();
		pageModel.setTotal(teachLst.getTotal());
		List<TeachDTO> dtos = new ArrayList<TeachDTO>();
		for (Teach news : (List<Teach>)teachLst.getDatas()) {
			TeachDTO dto = new TeachDTO();
			BeanUtils.copyProperties(news, dto, new String[]{"channel", "user", "releaseDate", "updateDate"});
			dto.setChannel(news.getChannel().getId());
			dto.setUser(news.getUser().getId());
			dto.setReleaseDate(DateUtil.format(news.getReleaseDate()));
			dto.setUpdateDate(DateUtil.format(news.getUpdateDate()));
			dtos.add(dto);
		}
		pageModel.setDatas(dtos);
		return JsonUtil.toStringFromObject(pageModel);
	}


	@Transactional
	public String addOrEdit(TeachDTO params) {
		Teach teach = null;
		if (params.getId() == null) {
			teach = new Teach();
			User user = (User)this.teachDao.findUniqueByProperty(User.class, "username", RequestUtil.getUsername());
			teach.setUser(user);
		} else {
			teach = (Teach)this.teachDao.get(Teach.class, params.getId());
		}
		teach.setAuthor(params.getAuthor());
		teach.setLevel(params.getLevel());
		teach.setReleaseDate(DateUtil.format(params.getReleaseDate()));
		teach.setUpdateDate(new Date());
		teach.setSource(params.getSource());
		teach.setState(params.getState());
		teach.setTitle(params.getTitle());
		teach.setTxt(params.getTxt());
		Channel channel = (Channel)this.teachDao.get(Channel.class, params.getChannel());
		teach.setChannel(channel);
		this.teachDao.addOrEdit(teach);
		return JsonUtil.toRes("保存成功！");
	}

	@Transactional
	public String remove(Integer id) {
		Teach teach = (Teach)this.teachDao.get(Teach.class, id);
		this.teachDao.remove(teach);
		return JsonUtil.toRes("删除成功！");
	}
	
	@Transactional(readOnly = true)
	public String getChannel() {
		Channel chan = (Channel)this.teachDao.findUniqueByProperty(Channel.class, "namespace", ElementConst.JKYCONST);
		List<Channel> channels = this.teachDao.findAll(Channel.class, null, Restrictions.eq("parent.id", chan.getId()));
		List<ComboDTO> dtos = new ArrayList<ComboDTO>();
		for (Channel channel : channels) {
			ComboDTO dto = new ComboDTO();
			dto.setId(channel.getId());
			dto.setStr(channel.getName());
			dtos.add(dto);
		}
		return JsonUtil.toString(dtos);
	}

	@Transactional(readOnly = true)
	public String getUser() {
		List<User> users = this.teachDao.findAll(User.class, null);
		List<ComboDTO> dtos = new ArrayList<ComboDTO>();
		for (User user : users) {
			ComboDTO dto = new ComboDTO();
			dto.setId(user.getId());
			dto.setStr(user.getTruename());
			dtos.add(dto);
		}
		return JsonUtil.toString(dtos);
	}

}
